package hangler

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"horizon-ai/cache"
	"horizon-ai/models"
	"horizon-ai/pkg"
	"strconv"
	"time"
)

func User(c *gin.Context) {
	name := c.PostForm("name")
	password := c.PostForm("password")
	if name == "" || password == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段为空",
		})
		return
	}
	data := models.User{
		Name:     name,
		Password: pkg.Md5Str(password),
	}
	redis_key := "user" + name
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Int()
	if cacheData >= 3 {

		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "密码错误3次 禁止登录",
		})
		return
	}
	err := models.DB.Where("name = ?", name).Find(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "登录失败",
		})
		return
	}
	if data.ID == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "未找到该用户",
		})
		return
	}
	if pkg.Md5Str(password) != data.Password {
		cache.Rdb.Incr(cache.Ctx, redis_key)
		if cacheData == 2 {
			cache.Rdb.Set(cache.Ctx, redis_key, 3, time.Hour*2)
			c.JSON(200, gin.H{
				"code": 500,
				"msg":  "密码错误3次 禁止登录",
			})
			return
		}
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "登陆成功",
	})
	return
}

func List(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "2"))
	offset := (page - 1) * size

	var posts models.Posts
	redis_key := "list" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Offset(offset).Limit(size).Find(&posts)
		jsonData, _ := json.Marshal(&posts)
		cache.Rdb.Set(cache.Ctx, cacheData, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &posts)
	}
	var total int64
	models.DB.Model(&models.Posts{}).Count(&total)

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": gin.H{
			"total": total,
			"posts": posts,
		},
	})
	return
}

func Add(c *gin.Context) {
	title := c.PostForm("title")
	content := c.PostForm("content")
	img := c.PostForm("img")
	data := models.Posts{
		Title:   title,
		Content: content,
		Img:     img,
	}
	models.DB.Create(&data)
	redis_key := "posts" + strconv.Itoa(int(data.ID))
	var posts models.Posts
	models.DB.Where("id = ?", data.ID).Find(&posts)
	jsonData, _ := json.Marshal(posts)
	cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return

}

func Del(c *gin.Context) {
	id := c.Query("id")

	models.DB.Where("id = ?", id).Delete(&models.Posts{})
	redis_key := "posts" + id
	cache.Rdb.Del(cache.Ctx, redis_key)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "删除成功",
	})
	return
}

func Detail(c *gin.Context) {
	id := c.Query("id")
	var posts models.Posts
	redis_key := "detail" + id
	jsonData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if jsonData == "" {
		models.DB.Where("id = ?", id).Find(&posts)
		marshal, _ := json.Marshal(posts)
		cache.Rdb.Set(cache.Ctx, redis_key, marshal, time.Hour*2)
	} else {
		json.Unmarshal([]byte(jsonData), &posts)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "进入详情成功",
		"data": posts,
	})
}
